package org.lilred.nzbberry.views;

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.BasicEditField;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.VerticalFieldManager;

import org.lilred.nzbberry.Themes;
import org.lilred.nzbberry.config.Resizer;
import org.lilred.ui.AccordionManager;
import org.lilred.ui.ClickableThemedField;
import org.lilred.ui.ClickableThemedLabelField;
import org.lilred.ui.CollapseableThemedField;
import org.lilred.ui.DecoratedInputField;
import org.lilred.ui.ThemedLabelField;
import org.lilred.ui.theme.ThemeOld;

public class ConfigView extends PageView {
	AccordionManager accordionManager;
	public PageView warningsManager = new PageView(Resizer.resize(3));
	public LabelField addressLabel = new LabelField("SABnzbd Address:");
	public ValidatedInputField addressManager;  
	public EditField addressEditField = new EditField(EditField.NO_NEWLINE) {
		protected void onUnfocus() {
			fieldChangeNotify(1);
			super.onUnfocus();
		}
	};
	public LabelField apiKeyLabel = new LabelField("API Key:");
	public ValidatedInputField apiKeyManager;
	public EditField apiKeyEditField = new EditField(EditField.NO_NEWLINE) {
		protected void onUnfocus() {
			fieldChangeNotify(1);
			super.onUnfocus();
		}
	};
	public ClickableThemedField validateButton;
	public ClickableThemedField clearWarningsButton;
	public ClickableThemedField refreshWarningsButton;
	public CollapseableThemedField warningsSettings;
	
	public ConfigView() {
		
		// Accordion Manager that holds all settings
		ThemeOld configTheme = AppView.getInstance().themeOld;
		accordionManager = new AccordionManager(configTheme, USE_ALL_HEIGHT) {
			protected void paint(Graphics g) {
				g.setBackgroundColor(0xdedfde);
				g.clear();
				super.paint(g);
			}
		};
		accordionManager.setCollapseable(false);
		PageView serverSettingsView = new PageView(Resizer.resize(3));
		ClickableThemedLabelField serverSettingsLabel = new ClickableThemedLabelField("Server Settings", 5, Themes.CLICKABLE_DEFAULT, Themes.CLICKABLE_FOCUS, Field.USE_ALL_WIDTH);
		CollapseableThemedField serverSettings = new CollapseableThemedField(serverSettingsLabel, serverSettingsView);
		ClickableThemedLabelField providerSettingsLabel = new ClickableThemedLabelField("NZB Provider Settings", 5, Themes.CLICKABLE_DEFAULT, Themes.CLICKABLE_FOCUS, Field.USE_ALL_WIDTH);
		Manager providerSettingsManager = new PageView(Resizer.resize(3));
		CollapseableThemedField providerSettings = new CollapseableThemedField(providerSettingsLabel, providerSettingsManager);
		ClickableThemedLabelField warningsLabel = new ClickableThemedLabelField("Warnings", 5, Themes.CLICKABLE_DEFAULT, Themes.CLICKABLE_FOCUS, Field.USE_ALL_WIDTH);
		warningsSettings = new CollapseableThemedField(warningsLabel, warningsManager);

		accordionManager.add(serverSettings);
		accordionManager.add(providerSettings);
		accordionManager.add(warningsSettings);
		add(accordionManager);
		
		// Server Settings panel
		addressManager = new ValidatedInputField(addressLabel);
		serverSettingsView.add(addressManager);
		serverSettingsView.add(new DecoratedInputField(addressEditField, Resizer.resize(3), Themes.INPUT_DEFAULT, Themes.INPUT_FOCUS));

		apiKeyManager = new ValidatedInputField(apiKeyLabel);
		serverSettingsView.add(apiKeyManager);
		serverSettingsView.add(new DecoratedInputField(apiKeyEditField, Resizer.resize(3), Themes.INPUT_DEFAULT, Themes.INPUT_FOCUS));

		validateButton = new ClickableThemedLabelField("Validate Settings", Resizer.resize(3), Themes.CLICKABLE_DEFAULT, Themes.CLICKABLE_FOCUS, Field.FIELD_RIGHT);
		serverSettingsView.add(validateButton);
		
		// Warnings panel
		clearWarningsButton = new ClickableThemedLabelField("Clear Warnings", Resizer.resize(3), Themes.CLICKABLE_DEFAULT, Themes.CLICKABLE_FOCUS);
		refreshWarningsButton = new ClickableThemedLabelField("Refresh Warnings", Resizer.resize(3), Themes.CLICKABLE_DEFAULT, Themes.CLICKABLE_FOCUS);
	}
	
	public class ValidatedInputField extends HorizontalFieldManager {
		Field f;
		public ValidatedInputField(Field f) {
			super(USE_ALL_WIDTH);
			add(f);
		}
		
		public void setImage(Field f) {
			this.f = f;
			UiApplication.getUiApplication().invokeLater(new Runnable() {
				public void run() {
					if (getFieldCount() == 1) add(ValidatedInputField.this.f);
					else if (ValidatedInputField.this.f.getManager() != ValidatedInputField.this) replace(getField(1), ValidatedInputField.this.f);					
				}
			});
		}
	}
	
}
